home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / fndpth.com / FINDPATH.PAS next >
Encoding:
Pascal/Delphi Source File  |  1990-02-09  |  4.5 KB  |  134 lines

  1. {  This unit implements routines to access and manipulate the DOS    }
  2. {  environment variable PATH and to use the PATH to find a user      }
  3. {  specified file.  I thought I had seen routines to do this         }
  4. {  somewhere but couldn't find them when I went looking.  I hope     }
  5. {  that others will find them useful.  This is a first release and   }
  6. {  comments are welcome.  Address them to 70441,3321 on BPROGA or    }
  7. {  via Easyplex.                                                     }
  8. {                                                                    }
  9. {                                                                    }
  10. {  FindFirstPath and FindNextPath are modeled on TP's FindFirst      }
  11. {  and FindNext file finding routines.  They use a PathRec in place  }
  12. {  of a SearchRec and the test condition is  PathRec.CurDir = ''     }
  13. {  rather than DOSError <> 0 (See the source of FileFind, below).    }
  14. {                                                                    }
  15. {  FindFile uses FindFirstPath and FindNextPath to find a user       }
  16. {  specified file in the current directory or anywhere on the user's }
  17. {  PATH.  If the file is not fount, FindFile returns a null string.  }
  18. {                                                                    }
  19. {                                 Chris Nelson                       }
  20. {                                 70441,3321                         }
  21. {                                 900209                             }
  22. UNIT FindPath;
  23.  
  24. INTERFACE
  25.  
  26. USES DOS;
  27.  
  28. TYPE
  29.    PathRec = Record
  30.       EnvPath : String;
  31.       CurDir : PathStr;
  32.       Pt : INTEGER;
  33.    END;
  34.  
  35. PROCEDURE FindFirstPath(VAR PathIn : PathRec);
  36. PROCEDURE FindNextPath(VAR PathIn : PathRec);
  37. FUNCTION FindFile(FileName : PathStr) : PathStr;
  38. {                                                              $EJECT}
  39. {--------------------------------------------------------------------}
  40. {                                                                    }
  41. IMPLEMENTATION
  42.  
  43. FUNCTION FileExists(FileName : PathStr) : BOOLEAN;
  44.  
  45. VAR
  46.    TestFile : FILE OF BYTE;
  47.  
  48. BEGIN
  49.    Assign(TestFile,FileName);
  50.    {$I-}
  51.    RESET(TestFile);
  52.    {$I+}
  53.    FileExists :=  (IORESULT = 0);
  54. END;
  55. {                                                              $EJECT}
  56. {--------------------------------------------------------------------}
  57. {                                                                    }
  58. PROCEDURE FindFirstPath(VAR PathIn : PathRec);
  59.  
  60. BEGIN WITH PathIn DO BEGIN
  61.    EnvPath := GetEnv('Path');
  62.    IF LENGTH(EnvPath) = 0 THEN BEGIN
  63.       CurDir := FExpand('.\');
  64.    END
  65.    ELSE BEGIN
  66.       Pt := POS(';',EnvPath);
  67.       IF Pt > 0 THEN BEGIN
  68.          CurDir := FExpand(COPY(EnvPath, 1, Pt - 1));
  69.          EnvPath := COPY(EnvPath,Pt + 1,Length(EnvPath));
  70.       END
  71.       ELSE BEGIN
  72.          CurDir := FExpand(EnvPath);
  73.          EnvPath := '';
  74.       END
  75.    END;
  76.  
  77.    IF CurDir[Length(CurDir)] <> '\' THEN
  78.       CurDir := CONCAT(CurDir,'\');
  79.  
  80. END; {WITH}
  81. END; {PROCEDURE FindFirstPath}
  82. {                                                                    }
  83. {--------------------------------------------------------------------}
  84. {                                                                    }
  85. PROCEDURE FindNextPath(VAR PathIn : PathRec);
  86.  
  87. BEGIN WITH PathIn DO BEGIN
  88.    IF Length(EnvPath) = 0 THEN BEGIN
  89.       CurDir := '';
  90.       EXIT;
  91.    END;
  92.  
  93.    Pt := POS(';',EnvPath);
  94.    IF Pt > 0 THEN BEGIN
  95.       CurDir := FExpand(COPY(EnvPath, 1, Pt - 1));
  96.       EnvPath := COPY(EnvPath,Pt + 1,Length(EnvPath));
  97.    END
  98.    ELSE BEGIN
  99.       CurDir := FExpand(EnvPath);
  100.       EnvPath := '';
  101.    END;
  102.  
  103.    IF CurDir[Length(CurDir)] <> '\' THEN
  104.       CurDir := CONCAT(CurDir,'\');
  105.  
  106. END; {WITH}
  107. END; {PROCEDURE FindNextPath}
  108. {                                                              $EJECT}
  109. {--------------------------------------------------------------------}
  110. {                                                                    }
  111. FUNCTION FindFile(FileName : PathStr) : PathStr;
  112.  
  113. VAR
  114.    PathInfo : PathRec;
  115.  
  116. BEGIN
  117.    IF FileExists(CONCAT(FExpand('.\'),FileName)) THEN BEGIN
  118.       FindFile := CONCAT(FExpand('.\'),FileName);
  119.       EXIT;
  120.    END;
  121.  
  122.    FindFirstPath(PathInfo);
  123.    WHILE PathInfo.CurDir <> '' DO BEGIN
  124.        IF FileExists(CONCAT(PathInfo.CurDir,FileName)) THEN BEGIN
  125.           FindFile := CONCAT(PathInfo.CurDir,FileName);
  126.           EXIT;
  127.        END
  128.        ELSE
  129.           FindNextPath(PathInfo);
  130.    END;{WHILE}
  131. END; {FUNCTION FindFile}
  132.  
  133. END. {UNIT FindPath}
  134.